﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SCMS.Model;

namespace SCMS.CoreBusinessLogic._WareHouse
{
    public class WareHouseService : IWareHouseService
    {
        private _Location.LocationService locationObj;
        private _CountrySubOffice.CountrySubOfficeService ctrySubOffObj;

        public WareHouseService()
        {
            locationObj = new _Location.LocationService();
            ctrySubOffObj = new _CountrySubOffice.CountrySubOfficeService();
        }

        public class WareHouseView
        {
            /// <summary>
            /// CountrySubOffice Entity
            /// </summary>
            public CountrySubOffice countrySubOffice;

            /// <summary>
            /// Location Entity
            /// </summary>
            public Location location;

            /// <summary>
            /// Country Entity based on location
            /// </summary>
            public Country country;

            /// <summary>
            /// WareHouse Entity
            /// </summary>
            public WareHouse wareHouse;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public WareHouse GetWareHouse(Guid id)
        {
            using (var dbContext = new SCMSEntities())
            {
                return dbContext.WareHouses.Where(c => c.Id.Equals(id)).FirstOrDefault<WareHouse>();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="search"></param>
        /// <returns></returns>
        public List<WareHouse> GetWareHouses(Guid countryProgId, string search = null)
        {
            List<WareHouse> wareHouseeList = new List<WareHouse>();
            using (var dbContext = new SCMSEntities())
            {
                if (string.IsNullOrEmpty(search))
                {
                    wareHouseeList = dbContext.WareHouses.Where(w => w.CountryProgrammeId == countryProgId).ToList<WareHouse>();
                }
                else
                {
                    wareHouseeList = dbContext.WareHouses
                        .Where(c => c.CountryProgrammeId == countryProgId && c.Name.Contains(search))
                        .OrderBy(c => c.Name).ToList<WareHouse>();
                }
            }
            return wareHouseeList;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="search"></param>
        /// <returns></returns>
        public List<WareHouseView> GetWareHouses1(Guid countryProgId, string search = null)
        {
            using (var dbContext = new SCMSEntities())
            {
                var wareHouseList = from warehouse in dbContext.WareHouses.Where(w => w.CountryProgrammeId == countryProgId)
                                    select warehouse;

                if (!string.IsNullOrEmpty(search))
                {
                    wareHouseList = from warehse in wareHouseList
                                    where warehse.Name.Contains(search)
                                    select warehse;
                }

                return (from whse in wareHouseList
                        select new WareHouseView
                                  {
                                      countrySubOffice = whse.CountrySubOffice,
                                      location = whse.Location,
                                      country = whse.Location.Country,
                                      wareHouse = whse
                                  }
                        ).ToList();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="wareHouse"></param>
        /// <returns></returns>
        public bool AddWareHouse(WareHouse wareHouse)
        {
            bool isSaved = false;

            if (wareHouse.Id.Equals(Guid.Empty))
            {
                wareHouse.Id = Guid.NewGuid();
            }

            using (var dbContext = new SCMSEntities())
            {
                dbContext.WareHouses.AddObject(wareHouse);
                if (dbContext.SaveChanges() > 0)
                {
                    isSaved = true;
                }
            }
            return isSaved;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="wareHouse"></param>
        /// <returns></returns>
        public bool EditWareHouse(WareHouse wareHouse)
        {
            bool isSaved = false;
            using (var dbContext = new SCMSEntities())
            {
                dbContext.WareHouses.Attach(wareHouse);
                dbContext.ObjectStateManager.ChangeObjectState(wareHouse, System.Data.EntityState.Modified);

                if (dbContext.SaveChanges() > 0)
                {
                    isSaved = true;
                }
            }
            return isSaved;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteWareHouse(Guid id)
        {
            bool isDeleted = false;
            using (var dbContext = new SCMSEntities())
            {
                WareHouse wareHouse = dbContext.WareHouses.Single(c => c.Id.Equals(id));
                dbContext.WareHouses.DeleteObject(wareHouse);
                if (dbContext.SaveChanges() > 0)
                {
                    isDeleted = true;
                }
            }
            return isDeleted;
        }

        public _Location.LocationService LocationObj
        {
            get
            {
                return locationObj;
            }
        }

        public _CountrySubOffice.CountrySubOfficeService CtrySubOffObj
        {
            get
            {
                return ctrySubOffObj;
            }
        }
    }
}
